package com.xpgk.mvc.organization;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.xpgk.dto.RenderBean;
import com.xpgk.dto.ZtreeNode;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.user.User;
import com.xpgk.tools.ToolRandoms;

public class OrganizationController extends BaseController {

	private OrganizationService organizationService;

	public void index() {
		Organization  organ =  Organization.dao.findById(getCUserOrganIds());
		setAttr("level", organ.getInt(Organization.column_level));
		render("organization.html");
	}

	/**
	 * 机构tree节点数据
	 */
	public void treeData() {
		List<ZtreeNode> nodeList = organizationService.childNodeData(getCxt(), ids, getCUserOrganIds());
		for (ZtreeNode ztreeNode : nodeList) {
			ztreeNode.setOpen("true");
		}
		renderJson(nodeList);
	}
	
	/**
	 * 跳转至添加
	 */
	public void toAdd() {
		Organization  organ =  Organization.dao.findById(ids);
		setAttr("organ", organ);
		setAttr("curOrgan", Organization.dao.findById(getCUserOrganIds()));
		String organIds = ToolRandoms.getUuid(true);
		setAttr("organIds", organIds);
		if(organ.getInt(Organization.column_level)==1){
			render("addOrgan1.html");
		}else if(organ.getInt(Organization.column_level)==2){
			render("addOrgan2.html");
		}else if(organ.getInt(Organization.column_level)==3){
			render("addOrgan3.html");
		}
		
		
	}

	/**
	 * 添加权限tree节点
	 */
	public void rightTreeData() {
		List<ZtreeNode> nodeList = organizationService.rightChildNodeData(getCxt(), ids, getParaToInt("level"));
		renderJson(nodeList);
	}

	@Before(OrganizationValidator.class)
	public void addOrgan() throws Exception{
		List<ZtreeNode> nodes = getBeans(ZtreeNode.class);
		Organization organ = getModel(Organization.class);
		User user = getModel(User.class);
		RenderBean renderBean = organizationService.addOrgan(nodes, organ, user,getPara("organIds"));
		renderJson(renderBean);
	}
	
	


	/**
	 * 跳转至更新
	 */
	public void toUpdate() {
		Organization organ =  Organization.dao.findById(ids);
		setAttr("organ",organ );
		setAttr("curOrganIds", getCUserOrganIds());
		if(organ.getInt(Organization.column_level)==1){
			render("upOrgan1.html");
		}else if(organ.getInt(Organization.column_level)==3){
			render("upOrgan2.html");
		}else if(organ.getInt(Organization.column_level)==4){
			render("upOrgan3.html");
		}
		//render("upOrgan.html");
	}
	
	/**
	 * 修改机构
	 */
	@Before(OrganizationValidator.class)
	public void upOrgan() {
		List<ZtreeNode> changeNodes = getBeans(ZtreeNode.class);
		Organization organ = getModel(Organization.class);
		User user = getModel(User.class);
		RenderBean renderBean = organizationService.upOrgan(changeNodes, organ, user);
		renderJson(renderBean);
	}
	
	/**
	 * 修改权限tree节点
	 */
	public void upRightTreeData() {
		List<ZtreeNode> nodeList = organizationService.upRightChildNodeData(getCxt(), ids, getParaToInt("level"), getPara("stationIds"));
		renderJson(nodeList);
	}

	
	/**
	 * 修改权限tree节点
	 */
	public void upRightTreeData2() {
		String organIds =  getPara("stationIds");
		String stationIds = "";
		String sqlstationIds = "SELECT sds.ids FROM sys_dept_station sds WHERE sds.organIds =?";
		if(Db.findFirst(sqlstationIds,organIds)!=null){
			stationIds = Db.findFirst(sqlstationIds,organIds).getStr("ids");
		}
		
		List<ZtreeNode> nodeList = organizationService.upRightChildNodeData(getCxt(), ids, getParaToInt("level"),stationIds);
		renderJson(nodeList);
	}

	/**
	 * 市场负责人下拉tree
	 */
	public void mangerSelect() {
		List<ZtreeNode> nodeList = organizationService.allOrganUserList(getCxt(), ids, getPara("organIds"));
		renderJson(nodeList);
	}
	
	/**
	 * 价格表下拉tree
	 */
	public void priceSelect(){
		//
		List<ZtreeNode> nodeList = organizationService.allOrganPricList(getCxt(), getCUserOrganIds() ,getPara("organIds"));
		renderJson(nodeList);
	}

	/**
	 * 查看用户信息
	 */
	public void lookUserInfo() {
		User user = User.dao.findById(ids);
		setAttr("userInfo", user);
		render("userInfo.html");
	}

	/**
	 * 机构用户列表
	 */
	public void organUserList() {
		String json = paging(splitPage, User.sqlId_splitPageSelect, User.sqlId_splitPageFrom);
		renderJson(json);
	}

	/**
	 * 机构列表
	 */
	public void organList() {
		String parentIds = (String) splitPage.getQueryParam().get("parentIds");
		if (ZBIDS.equals(parentIds)) {
			splitPage.getQueryParam().remove("parentIds");
			splitPage.getQueryParam().put("level", "2");
		}
		String json = paging(splitPage, Organization.sqlId_splitPageSelect, Organization.sqlId_splitPageFrom);
		renderJson(json);
	}
	
	/**
	 * 删除机构
	 */
	public void delOrgan(){
		String result = organizationService.delete(ids);
		renderText(result);
	}

}
